<?php
/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */

define('IN_ADMIN_COMPONENT_CONTROLLER', 'true');

require_once('includes/components/LanguagesComponent.php');

class LanguagesAdministrationComponent extends LanguagesComponent
{
	function LanguagesAdministrationComponent()
	{
	}

	function action(&$params, $action)
	{
		if ($action == 'Update') 
			return $this->edit($params);
		else
			return $this->query($params);
	}

	function edit(&$params) 
	{
		$i = 0;
		while (true)
		{
			$i++;
			if ($i > 100) break;

			$languageId = (int)$params['languageId-'.$i];
			if ($languageId == 0) break;

			$languageName = prepare_input($params['languageName-'.$i]);
			if ($languageName == null || strlen($languageName) == 0) continue;
			$languageCode = prepare_input($params['languageCode-'.$i]);
			if ($languageCode == null || strlen($languageCode) == 0) continue;
			$languageImage = prepare_input($params['languageImage-'.$i]);
			$languageSortOrder = (int)$params['languageSortOrder-'.$i];
					
			$deleteRow = (boolean)$params['languageDelete-'.$i];
					
			if ($deleteRow) {
				if ($languageId < 1) continue;
				$sql = 'delete from ' . TABLE_LANGUAGES . ' where languages_id=' . $languageId;
				mysql_query($sql) or die(mysql_error());
			}
			else {
				$sql_data_array = array();
				$sqlAction = 'insert';
				$where = '';
				if ($languageId > 0) {
					$sqlAction = 'update';
					$where = 'languages_id=' . $languageId;
				}
				else {
					$sql_data_arry['languages_id']=$languageId;
				}
				$sql_data_array['name'] = $languageName;
				$sql_data_array['code'] = $languageCode;
				$sql_data_array['image'] = $languageImage;
				$sql_data_array['directory'] = "no longer used";
				$sql_data_array['sort_order'] = $languageSortOrder;
				db_perform(TABLE_LANGUAGES, $sql_data_array, $sqlAction, $where);
			}
		}
		
		clean_cache();
		return $this->query($params);
	}
}
?>
